/**
 * 处理编辑按钮与自定义按钮
 */
import Util from '@/js/Util.js'
import FormConstant  from "./FormConstant";

export default {
    methods:{
        /**
         * 根据绑定配置，将返回的值绑定到表单。
         * @param binding
         * @param rows
         * @param flag 是否清空之前的绑定
         * @param data 当前表数据 未传默认为this.formVm.data
         * @param formVm 当前表单实例
         * Callback 回调函数
         */
        bindRows(binding, rows, flag,data,formVm,callback){
            let gridName = binding.gridName;
            let returnFields = binding.returnFields;
            if(!returnFields || returnFields.length==0){
                return;
            }

            //按钮在主表或一对一子表中
            if(binding.curType=="main" || binding.curType=="oneTable"){
                if(!data){
                    data=this.formVm.data;
                }
                //主表
                if(gridName=="curForm"){
                    let newRow=this.handRowData(rows,returnFields);
                    Object.assign(data,newRow);
                }else {
                    let uniqueField=binding.uniqueField;
                    let subTableData=data[gridName];
                    //一对多子表
                    if(subTableData instanceof Array){
                        for (let i = 0; i < rows.length; i++) {
                            if(!(!flag && binding.superaddition)){
                                subTableData.length = 0 ;
                            }
                            let row = rows[i];
                            let newRow = this.handOneRow(row, returnFields,gridName,{});
                            let canAdd = this.canAddRow(uniqueField, newRow, subTableData);
                            if (!canAdd) {
                                continue;
                            }
                            let key = Util.randomId();
                            newRow.index_ = key;
                            newRow.selected = false;
                            let obj = {_key:key,...newRow};
                            subTableData.push(obj);
                        }
                    }
                    //一对一。
                    else{
                        if(!subTableData){
                            subTableData = this.data ;
                        }
                        let newRow=this.handOneRow(rows[0],returnFields,gridName,subTableData);
                        Object.assign(subTableData,newRow);
                    }
                }
            }else{
                if(!data){
                    data=this.data;
                }

                //多选
                if(binding.singleType=="multi"){
                    let newRow=this.handRowData(rows,returnFields,gridName,data);
                    Object.assign(data,newRow);
                }else {
                    let newRow=this.handOneRow(rows[0],returnFields,gridName,data);
                    Object.assign(data,newRow);
                }
            }
            if(callback){
                callback()
            }
        },
        /**
         * 处理数据。主表、子表一行
         */
        handRowData(rtnRow,returnFields){
            let newRow={};
            for(let k = 0, l = returnFields.length; k < l; k++) {
                let field = returnFields[k];
                let valMode=field.valMode;
                let bindField=field.bindField;
                let bindType = field.bindType;
                if (bindType=="fixed") {
                    newRow[bindField] = field.returnValue;
                }else if (bindType=="script") {
                    if(this.formVm[field.returnValue]){
                        newRow[bindField] =this.formVm[field.returnValue](rtnRow[0]);
                    }
                }else{
                    let temp = [];
                    if(valMode=="double"){
                        let json={};
                        let labelArray = [];
                        let valueArray = [];
                        for(let i = 0; i < rtnRow.length; i++) {
                            labelArray.push(rtnRow[i][field.returnLabel.toUpperCase()]);
                            valueArray.push(rtnRow[i][field.returnValue.toUpperCase()]);
                        }
                        json.label = labelArray.join(',');
                        json.value = valueArray.join(',');
                        newRow[bindField] = JSON.stringify(json);
                    }
                    //多值情况
                    else if(valMode=="multi"){
                        let newValue={};
                        //地址
                        if(field.ctlType=="address"){
                            let obj=field.returnValue;
                            for(let key in obj){
                                newValue[key]=rtnRow[0][obj[key].toUpperCase()]
                            }
                        }
                        newRow[bindField] = JSON.stringify(newValue);
                    } else{
                        for(let i = 0; i < rtnRow.length; i++) {
                            //判断当前字段是否为多语言字段
                            if (field.isMultiLang){
                                temp.push(this.$i18(rtnRow[i][field.returnValue.toUpperCase()]))
                            }else {
                                temp.push(rtnRow[i][field.returnValue.toUpperCase()])
                            }

                        }
                        let returnRow = temp.join(',');
                        newRow[bindField] = returnRow;
                    }
                }
            }
            return newRow;
        },
        /**
         * 判断唯一字段值
         * @param uniqueField
         * @param newRow
         * @param subTableData
         * @returns {boolean}
         */
        canAddRow(uniqueField,newRow,subTableData){
            if(!uniqueField){
                return true;
            }
            let val=newRow[uniqueField];
            let ary= subTableData.filter(item=>{
                return item[uniqueField]==val;
            });
            if(ary.length==0){
                return true;
            }
            return false;
        },
        handOneRow(rtnRow,returnFields,gridName,curRow){
            let newRow="";
            if(this.formVm.data.initData){
                for (let key in this.formVm.data.initData) {
                    if(FormConstant.SubPrefix + key == gridName){
                        newRow=Object.assign({},this.formVm.data.initData[key],curRow);
                    }
                }
            }
            if(!newRow){
                newRow=curRow;
            }
            for(let k = 0, l = returnFields.length; k < l; k++) {
                let field = returnFields[k];
                let valMode=field.valMode;
                let bindField=field.bindField;
                let bindType = field.bindType;
                if (bindType=="fixed") {
                    newRow[bindField] = field.returnValue;
                }else if (bindType=="script") {
                    if(this.formVm[field.returnValue]){
                        newRow[bindField] =this.formVm[field.returnValue](rtnRow);
                    }
                }else{
                    if(valMode=="double"){
                        let json={};
                        json.label=rtnRow[field.returnLabel.toUpperCase()];
                        json.value=rtnRow[field.returnValue.toUpperCase()];
                        newRow[bindField]= JSON.stringify(json);
                    }else{
                        if(typeof field.returnValue === 'string'){
                            newRow[bindField]=rtnRow[field.returnValue.toUpperCase()];
                        }else if(Util.getObjType(field.returnValue) === 'object'){
                            let obj = {}
                            for (let key in field.returnValue){
                                obj[key] = rtnRow[field.returnValue[key].toUpperCase()]
                            }
                            newRow[bindField] = JSON.stringify(obj);
                        }
                    }
                }
            }
            return newRow;
        },
        /**
         * 判断对话框输入条件 是否必填
         * @param condition
         * @param data
         * @param row
         * @returns {boolean}
         */
        isConditionRequired(condition,data,row){
            if(!condition || (Array.isArray(condition) && condition.length === 0)){
                return false
            }
            for (let item of condition){
                if(item.required && item.bindVal){
                    let val = '';
                    if(item.table === 'curForm'){ //主表
                        val = data[item.bindVal]
                    }else {//子表或者一对一子表
                        val = row[item.bindVal]
                    }
                    //如果没有填值
                    if(typeof val !=='boolean' && !val && val!==0){
                        //`查询条件【${}】的绑定值不能为空！`
                        let  msg =this.$t('formLang.empty',[item.fieldName,item.ctlName])
                        this.$message.warning(msg)
                        return true
                    }
                }
            }
            return false
        },
        /**
         * 判断打开对话框的前置方法
         * @param beforeMethod 方法名称
         * @param formVm
         * @param data
         * @returns {boolean}
         */
        isBeforeMethod(beforeMethod){
            if(!this.formVm[beforeMethod]){
                this.$message.warning(this.$t('formLang.beforeMethod',['请检查js脚本中是否有设置前置方法!']))
                return true;
            }
            //增加前置方法.
            let isBefore = this.formVm[beforeMethod](this.data);
            if(isBefore && isBefore.hasOwnProperty('success')){
                if(!isBefore.success){
                    if(isBefore.msg){
                        this.$message.warning(isBefore.msg)
                    }else {
                        this.$message.warning(this.$t('formLang.noBeforeMethod',['未满足打开对话框所需的前置条件~']))
                    }
                    return true;
                }
            }
            return false
        }
    }
}
